package com.zhang.network.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.network.mapper.NetworkUserMapper;
import com.zhang.network.mapper.UserMapper;
import com.zhang.network.model.Friend;
import com.zhang.network.mapper.FriendMapper;
import com.zhang.network.model.Network;
import com.zhang.network.model.NetworkUser;
import com.zhang.network.model.User;
import com.zhang.network.model.friend.FriendQueryDTO;
import com.zhang.network.model.friend.FriendVO;
import com.zhang.network.model.network.NetWorkQueryDTO;
import com.zhang.network.service.FriendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.network.service.NetworkService;
import com.zhang.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhang
 * @since 2021-04-03
 */
@Slf4j
@Service
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend> implements FriendService {

    @Resource
    UserMapper userMapper;

    @Resource
    NetworkUserMapper networkUserMapper;

    @Resource
    NetworkService networkService;

    @Override
    public Page<FriendVO> pageFriend(FriendQueryDTO queryDTO) {
        Page<FriendVO> friendVOPage = new Page<>();
        QueryWrapper<Friend> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(queryDTO.getId())) {
            wrapper.eq("user_id", queryDTO.getId());
        }
        if (StringUtils.isNotBlank(queryDTO.getRecommendedId())) {
            wrapper.ne("friend_id", queryDTO.getRecommendedId());
        }

        if (StringUtils.isNotBlank(queryDTO.getNowId()) && (!queryDTO.getNowId().equals(queryDTO.getId()))) {
            List<Friend> myFriends = baseMapper.selectList(new QueryWrapper<Friend>().eq("user_id", queryDTO.getNowId()));
            List<Integer> list1 = myFriends.stream().map(friend -> friend.getFriendId()).collect(Collectors.toList());
            wrapper.notIn("friend_id", list1);
        }

        List<Friend> allFriends = baseMapper.selectList(wrapper);
        if (allFriends.size() <= 0) {
            return friendVOPage;
        }

        Page<User> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        List<Integer> list = allFriends.stream().map(friend -> friend.getFriendId()).collect(Collectors.toList());

        QueryWrapper<User> userWrapper = new QueryWrapper<>();
        if (list.size() <= 0) {
            return friendVOPage;
        }
        userWrapper.in("id", list);
        if (StringUtils.isNotBlank(queryDTO.getRemoveId())) {
            userWrapper.notIn("id", queryDTO.getRemoveId());
        }
        if (StringUtils.isNotBlank(queryDTO.getNickName())) {
            userWrapper.like("nick_name", queryDTO.getNickName());
        }
        if (StringUtils.isNotBlank(queryDTO.getProfession())) {
            userWrapper.like("profession", queryDTO.getProfession());
        }
        if (StringUtils.isNotBlank(queryDTO.getCity())) {
            userWrapper.like("city", queryDTO.getCity());
        }
        if (StringUtils.isNotBlank(queryDTO.getAccount())) {
            userWrapper.like("account", queryDTO.getAccount());
        }
        Page<User> userList = userMapper.selectPage(page, userWrapper);

        List<FriendVO> friendVOList = null;
        if (userList.getRecords().size() > 0) {
            friendVOList = userList.getRecords().stream().map(user -> {
                FriendVO friendVO = new FriendVO();
                BeanUtils.copyProperties(user, friendVO);
                List<Friend> friends = allFriends.stream().filter(friend->
                        friend.getFriendId().equals(user.getId())).collect(Collectors.toList());
                if (friends.size() > 0){
                    friendVO.setRank(friends.get(0).getRank());
                }
                return friendVO;
            }).collect(Collectors.toList());
            BeanUtils.copyProperties(userList, friendVOPage);
            friendVOPage.setRecords(friendVOList);

        }
        return friendVOPage;


    }

    @Override
    public Page<FriendVO> pageRankFriend(FriendQueryDTO queryDTO) {
        Page<FriendVO> friendVOPage = new Page<>();
        QueryWrapper<Friend> wrapper = new QueryWrapper<>();
        log.info("等级好友查询参数："+queryDTO.toString());
        if (StringUtils.isNotBlank(queryDTO.getId())) {
            wrapper.eq("user_id", queryDTO.getId());
        }
        if (StringUtils.isNotBlank(queryDTO.getRecommendedId())) {
            wrapper.ne("friend_id", queryDTO.getRecommendedId());
        }
        if (StringUtils.isNotBlank(queryDTO.getRank())) {
            wrapper.eq("rank", queryDTO.getRank());
        }
        List<Friend> allFriends = baseMapper.selectList(wrapper);

        if (allFriends.size() <= 0) {
            return friendVOPage;
        }

        Page<User> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        List<Integer> list = allFriends.stream().map(friend -> friend.getFriendId()).collect(Collectors.toList());
        QueryWrapper<User> userWrapper = new QueryWrapper<>();
        if (list.size() <= 0) {
            return friendVOPage;
        }
        userWrapper.in("id", list);
        if (StringUtils.isNotBlank(queryDTO.getNickName())) {
            userWrapper.like("nick_name", queryDTO.getNickName());
        }
        if (StringUtils.isNotBlank(queryDTO.getAccount())) {
            userWrapper.like("account", queryDTO.getAccount());
        }
        Page<User> userList = userMapper.selectPage(page, userWrapper);
        List<FriendVO> friendVOList = null;
        if (userList.getRecords().size() > 0) {
            friendVOList = userList.getRecords().stream().map(user -> {
                FriendVO friendVO = new FriendVO();
                BeanUtils.copyProperties(user, friendVO);
                friendVO.setRank(queryDTO.getRank());
                return friendVO;
            }).collect(Collectors.toList());
            BeanUtils.copyProperties(userList, friendVOPage);
            friendVOPage.setRecords(friendVOList);

        }
        return friendVOPage;

    }

    @Override
    public List<Network> getSameNetwork(Friend friend) {
        List<Network> resultList = new ArrayList<>();
        List<NetworkUser> networkUserList1 = networkUserMapper.selectList(new QueryWrapper<NetworkUser>().eq("user_id", friend.getUserId()));
        List<NetworkUser> networkUserList2 = networkUserMapper.selectList(new QueryWrapper<NetworkUser>().eq("user_id", friend.getFriendId()));
        List<Integer> networkList1 = networkUserList1.stream().map(NetworkUser::getNetworkId).collect(Collectors.toList());
        List<Integer> networkList2 = networkUserList2.stream().map(NetworkUser::getNetworkId).collect(Collectors.toList());
        List<Integer> list = networkList1.stream()
                .filter(list1 -> networkList2.contains(list1))
                .collect(Collectors.toList());
        if (list.size() > 0) {
            resultList = networkService.list(new QueryWrapper<Network>()
                    .in("id", list));
            return resultList;
        }
        return resultList;
    }


    @Override
    public boolean deleteFriend(Friend friend) {
        return baseMapper.delete(new UpdateWrapper<Friend>().eq("user_id", friend.getUserId()).eq("friend_id", friend.getFriendId())) != 0;
    }

}
